package com.dqx.haoqiu.repository.repository.user;

import com.dqx.haoqiu.repository.entity.TokenEntity;
import com.dqx.haoqiu.repository.entity.UserEntity;
import com.dqx.haoqiu.repository.repository.user.local.UserLocalSource;
import com.dqx.haoqiu.repository.repository.user.remote.UserRemoteSource;
import com.xmjs.baselibrary.utils.LocalCache;
import com.xmjs.baselibrary.utils.SP;

import javax.inject.Inject;

import io.reactivex.Observable;

/**
 * 用户信息相关的数据仓库
 */
public class UserRepository implements UserSource {
    private final UserLocalSource localSource;
    private final UserRemoteSource remoteSource;
    private SP mSP;

    @Inject
    public UserRepository(UserLocalSource localSource, UserRemoteSource remoteSource, SP sp) {
        this.localSource = localSource;
        this.remoteSource = remoteSource;
        mSP = sp;
    }

    @Override
    public Observable<TokenEntity> login(String username, String password) {
        return remoteSource.login(username, password).doOnNext(entity -> {
            //登录成功后，先本地保存令牌
            LocalCache.saveToken(mSP, entity.getAccess_token());
        });
    }

    @Override
    public Observable<UserEntity> getUserInfo() {
        return remoteSource.getUserInfo();
    }

    @Override
    public void setShowGuide(boolean showGuide) {
        localSource.setShowGuide(showGuide);
    }

    @Override
    public boolean showGuide() {
        return localSource.showGuide();
    }

    @Override
    public void setShowPrivaceDialog(boolean show) {
        localSource.setShowPrivaceDialog(show);
    }

    @Override
    public boolean showPrivaceDialog() {
        return localSource.showPrivaceDialog();
    }
}
